package com.bruce.demo2.patterns.demo1.creational.demo04.Prototype.demo3;

import java.io.*;

/**
 * @PROJECT_NAME design_patterns
 * @DESCRIPTION 奖状类: demo1浅拷贝的弊端
 * @AUTHOR CNBruceLee
 * @DATE 2021/7/11 22:21
 */
public class Citation implements Serializable, Cloneable {

    private Student student;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public void show() {
        System.out.println(this.student.getName() + " 同学：在2021学年第一学期中表现优秀，被评为三号学生，特发此状！！！");
    }

    @Override
    protected Citation clone() {
        Citation citation = null;
        try {
            //序列化对象
            String filePath = "D:\\workspace\\design_patterns\\src\\main\\java\\com\\bruce\\patterns\\creational\\demo4\\Prototype\\demo3\\a.txt";
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
            oos.writeObject(this);
            oos.close();

            //反序列化生产新的对象
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
            citation = (Citation) ois.readObject();
            ois.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return citation;
    }
}
